package com.start.common.enums;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class StartEnumSerializer extends JsonSerializer<Enum> {

    @Override
    public void serialize(Enum value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (!(value instanceof StartBaseEnum)) {
            return;
        }
        StartBaseEnum baseEnum = (StartBaseEnum) value;
        if (baseEnum instanceof StartBaseIntegerEnum) {
            gen.writeNumber(String.valueOf(baseEnum.getValue()));
        } else {
            gen.writeString(String.valueOf(baseEnum.getValue()));
        }
        gen.writeStringField(gen.getOutputContext().getCurrentName() + "_Label", baseEnum.getLabel());
    }
}
